const initRecord = function(manager,_this,pattern = undefined) {
  //有新的识别内容返回，则会调用此事件
  // manager.onRecognize = (res) => {
  // }
  manager.onStart = (res) => {
    _this.setData({
      speakOn: true
    })
    console.log("===开始录音")
  }
  // 识别结束事件
  manager.onStop = (res) => {
    let text = res.result
    if(pattern != undefined) {
      let reg = text.match(pattern)
      if(reg == null) {
        wx.showModal({
          title: '提示',
          content: '请按照格式输入语音',
          showCancel: false
        })
        return
      }
      pattern.exec(text)
      _this.setData({
        address: RegExp.$1.indexOf('，') != -1 ? RegExp.$1.substring(0,RegExp.$1.indexOf('，')) : RegExp.$1,
        recordContent: RegExp.$2,
        contentLength: parseInt(RegExp.$2.length)
      })
    } else {
      if(text == '') {
        // 用户没有说话，可以做一下提示处理...
        wx.showModal({
          title: '提示',
          content: '不好意思，没有听清',
          showCancel: false
        })
        return
      }
      _this.setData({
        recordContent: text,
        contentLength: parseInt(text.length)
      })
    }
    
  }
}

const streamRecord = function(manager,_this) {
  manager.start({
    lang: 'zh_CN',
  })
}

const endStreamRecord = function(manager,_this) {
  manager.stop()
  _this.setData({
    speakOn: false,
    audioOn: false
  })
}


module.exports = {
  initRecord: initRecord,
  streamRecord: streamRecord,
  endStreamRecord: endStreamRecord 
}